﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using DevExpress.XtraEditors;

namespace LabLibary
{
    public static class LabCommon
    {
        public static void ShowMessage(string message)
        {
            XtraMessageBox.Show(message);
        }

        public static void InitLabMachine(ComboBoxEdit cbLabMachine)
        {
            for (int i = 1; i <= LabMachines.lstLabMachines.Count; i++)
            {
                cbLabMachine.Properties.Items.Add(LabMachines.lstLabMachines[i]);
            }
        }

        public static string[] GetAvailablePortName(){
            return SerialPort.GetPortNames();
        }

        public static void InitListPortName(ComboBoxEdit cbLstComprt)
        {
            string[] comports = GetAvailablePortName();

            foreach (string comport in comports)
            {
                cbLstComprt.Properties.Items.Add(comport);
            }
        }

        public static void InitSpeed(ComboBoxEdit cbLstSpeeds)
        {
            string[] speeds = {"75", "110", "134", "150", "300", "600", "1200", "1800", "2400", "4800", "7200", "9600", "14400", "19200", "38400", "57600", "115200", "128000"};

            foreach (string spd in speeds)
            {
                cbLstSpeeds.Properties.Items.Add(spd);
            }
        }

        public static void InitDataBits(ComboBoxEdit cbLstDataBits)
        {
            string[] dataBits = { "4", "5", "6", "7", "8" };
            foreach (string bit in dataBits)
            {
                cbLstDataBits.Properties.Items.Add(bit);
            }
        }

        public static void InitParity(ComboBoxEdit cbLstParity)
        {
            string[] parity = { "Even", "Odd", "None", "Mark", "Space" };
            foreach (string p in parity)
            {
                cbLstParity.Properties.Items.Add(p);
            }
        }

        public static void InitStopBits(ComboBoxEdit cbControl)
        {
            string[] _stopbits = { "1", "2" };
            foreach (string _bit in _stopbits)
            {
                cbControl.Properties.Items.Add(_bit);
            }
        }

        public static void InitHandShaking(ComboBoxEdit cbControl)
        {
            string[] _handshaking = {Handshake.None.ToString(), Handshake.RequestToSend.ToString(), Handshake.RequestToSendXOnXOff.ToString(), Handshake.XOnXOff.ToString() };

            foreach (string strHandSharking in _handshaking)
            {
                cbControl.Properties.Items.Add(strHandSharking);
            }
        }



        public static byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }

        public static void AutoSend(SerialPort _comport)
        {
            try
            {
                // Convert the user's string of hex digits (ex: B4 CA E2) to a byte array
                byte[] data = HexStringToByteArray("06");

                // Send the binary data out the port
                _comport.Write(data, 0, data.Length);
            }
            catch (FormatException) {}
        }
    }
}
